﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Common;
using Automobile.service.Model;
using System.Data;
using System.Data.SqlClient;
namespace Automobile.service.DAL
{
    
   public class PurchaseOutDAL
    {
       SQLHelper h = new SQLHelper();
       /// <summary>
       /// 采购订单分页显示
       /// </summary>
       /// <param name="Pageindex"></param>
       /// <param name="PageSize"></param>
       /// <returns></returns>
       public List<Purchase>Purchase_Show(int Pageindex,int PageSize,out int count)
       {
           string sql = "Purchase_Page";
           SqlParameter[] par = new SqlParameter[] { 
           new SqlParameter("@index",Pageindex),
           new SqlParameter("@size",PageSize),
           new SqlParameter("@count",SqlDbType.Int),
           };
           par[2].Direction = ParameterDirection.Output;
           
           DataTable table = h.ExecDataTable_Proc(sql,par);
           List<Purchase> list = new List<Purchase>();
           foreach (DataRow item in table.Rows)
           {
               Purchase P = new Purchase();
               P.Employee_Id = Convert.ToInt32(item["Employee_Id"]);

               P.Purchase_curom =item["Purchase_curom"].ToString();


               P.Purchase_date = Convert.ToDateTime(item["Purchase_date"]);
               P.Purchase_Id = Convert.ToInt32(item["Purchase_Id"]);
               P.Purchase_Remark = item["Purchase_Remark"].ToString();
               P.Purchase_State = Convert.ToInt32(item["Purchase_State"]);
               P.SumMoney = Convert.ToInt32(item["SumMoney"]);
               P.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
               P.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
               P.WareHose_Name = item["WareHose_Name"].ToString();
               P.Supplier_Name = item["Supplier_Name"].ToString();
               P.Employee_Name = item["Employee_Name"].ToString();
               list.Add(P);
           }
           count = Convert.ToInt32(par[2].Value);
           return list;
       }
       /// <summary>
       /// 获取总数量
       /// </summary>
       /// <returns></returns>
       public int count()
       {
           string str = "select count(*) from Purchase";
           return Convert.ToInt32(h.ExecScalar(str));
       }

       /// <summary>
       /// 添加采购返厂单信息
       /// </summary>
       /// <param name="P"></param>
       /// <param name="xml"></param>
       /// <returns></returns>
       public int Purchase_Add(Purchase P,string xml)
       {
           string str = "Purchase_Add";
           SqlParameter[] par = new SqlParameter[] { 
           
                       new SqlParameter("@Purchase_date",P.Purchase_date),
                       new SqlParameter("@SumMoney",P.SumMoney),
                       new SqlParameter("@Purchase_Remark",P.Purchase_Remark),
                       new SqlParameter("@Supplier_Id",P.Supplier_Id),
                       new SqlParameter("@WareHose_Id",P.WareHose_Id),
                       new SqlParameter("@Employee_Id",P.Employee_Id),
                       new SqlParameter("@PurChase_InOut",P.PurChase_InOut),
                       new SqlParameter("@xml",xml),
           
           };
           return h.ExecNonquery_Proc(str, par);
       }
       /// <summary>
       /// 显示商品信息
       /// </summary>
       /// <param name="Product_Modelspe"></param>
       /// <returns></returns>
       public DataTable Product_Sel(string Product_Modelspe)
       {
           StringBuilder str = new StringBuilder();
           str.Append("Product_Sel");
           SqlParameter[] par = new SqlParameter[] { 
           
              new SqlParameter("@Product_Modelspe",Product_Modelspe),
           };
           return h.ExecDataTable_Proc(str.ToString(), par);
       }
       /// <summary>
       /// 审核成功库存改变
       /// </summary>
       /// <param name="Purchase_Id"></param>
       /// <returns></returns>
       public int Product_Sate(int Purchase_Id)
       {
           StringBuilder str = new StringBuilder();
           str.Append("Proc_palautus");
           SqlParameter[] Par = new SqlParameter[] { 
           
                       new SqlParameter("@Purchase_Id",Purchase_Id)
           
           };
           return h.ExecNonquery_Proc(str.ToString(), Par);

       }
 /// <summary>
 /// 审核出库
 /// </summary>
 /// <param name="Purchase_Id"></param>
 /// <returns></returns>
       public List<Purchase> Purchase_palauat(int Purchase_Id)
       {
           StringBuilder str = new StringBuilder();
           str.Append("Pro_Out");
           SqlParameter[] Par = new SqlParameter[] { 
           
                       new SqlParameter("@Purchase_Id",Purchase_Id)
           
           };
           DataTable table = h.ExecDataTable_Proc(str.ToString(), Par);
           List<Purchase> list = new List<Purchase>();
           foreach (DataRow item in table.Rows)
           {
               Purchase P = new Purchase();
               P.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
               P.Purchase_curom = item["Purchase_curom"].ToString();
               P.Purchase_date = Convert.ToDateTime(item["Purchase_date"]);
               P.Purchase_Id = Convert.ToInt32(item["Purchase_Id"]);
               P.Purchase_Remark = item["Purchase_Remark"].ToString();
               P.Purchase_State = Convert.ToInt32(item["Purchase_State"]);
               P.SumMoney = Convert.ToInt32(item["SumMoney"]);
               P.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
               P.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
               P.WareHose_Name = item["WareHose_Name"].ToString();
               P.Supplier_Name = item["Supplier_Name"].ToString();
               P.Employee_Name = item["Employee_Name"].ToString();
               P.PurChase_InOut = item["PurChase_InOut"].ToString();
               list.Add(P);
           }
           return list;
       }
       // <summary>
       /// 审核出库
       /// </summary>
       /// <param name="Purchase_Id"></param>
       /// <returns></returns>
       public List<PurchaseMate> Purchase_pal(int Purchase_Id)
       {
           StringBuilder str = new StringBuilder();
           str.Append("Pro_selter");
           SqlParameter[] Par = new SqlParameter[] { 
           
                       new SqlParameter("@Purchase_Id",Purchase_Id)
           
           };
           DataTable table = h.ExecDataTable_Proc(str.ToString(), Par);
        List<PurchaseMate> list = new List<PurchaseMate>();
        foreach (DataRow item in table.Rows)
        {
            PurchaseMate P = new PurchaseMate();
            P.PurchaseMate_Num = Convert.ToInt32(item["PurchaseMate_Num"]);
            P.PurchaseMate_Price = Convert.ToDecimal(item["PurchaseMate_Price"]);
            P.Product_Name = item["Product_Name"].ToString();
            P.Product_Bprid = Convert.ToDecimal(item["Product_Bprid"]);
            P.Product_Price = Convert.ToDecimal(item["Product_Price"]);
            P.Product_Modelspe = item["Product_Modelspe"].ToString();
            P.Product_Barcode = item["Product_Barcode"].ToString();
            P.Product_frim = item["Product_frim"].ToString();
            list.Add(P);
        }
        return list;
       }
    }

}
